package com.lemon.service;

import java.util.Date;
import java.util.List;

import com.lemon.dao.RoleDao;
import com.lemon.domain.Authorize;
import com.lemon.domain.Employee;
import com.lemon.domain.Role;

public class RoleService {

	private RoleDao dao = new RoleDao();

	public Role get(Long id) {
		return dao.get(id);
	}
	
	public List<Role> list() {
		return dao.list();
	}
	
	public Long save(Role r) {
		r.setCreateTime(new Date());
		Authorize authorize = new Authorize();
		//向数据库中插入1条新的权限记录并且返回该记录的id
		Long authorizeId = new AuthorizeService().save(authorize);
		//将 authorizeId 设置到 role 中
		r.setAuthorizeId(authorizeId);
		return dao.save(r);
	}
	
	public int delete(Long id) {
		return dao.delete(id);
	}
	
	public int update(Role r) {
		r.setUpdateTime(new Date());
		return dao.update(r);
	}
	
	/**
	 * 为角色添加成员
	 * @param roleId 角色ID
	 * @param memberId 成员们的ID
	 */
	public void addMember(Long roleId, Long[] memberId) {
		EmployeeService employeeService = new EmployeeService();
		
		for (Long id : memberId) {
			Employee e = new Employee();
			e.setId(id);
			e.setRoleId(roleId);
			employeeService.addRole(e);
		}
	}
}
